<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 清空缓存
        app()['cache']->forget('spatie.permission.cache');

        // 添加权限
        $permissions = [
            ['name' => 'users.index', 'cn_name' => '用户列表', 'guard_name' => 'api'],
            ['name' => 'users.show', 'cn_name' => '用户详情', 'guard_name' => 'api'],
            ['name' => 'users.lock', 'cn_name' => '用户禁用启用', 'guard_name' => 'api'],

            ['name' => 'category.store', 'cn_name' => '添加分类', 'guard_name' => 'api'],
            ['name' => 'category.index', 'cn_name' => '分类列表', 'guard_name' => 'api'],
            ['name' => 'category.show', 'cn_name' => '分类详情', 'guard_name' => 'api'],
            ['name' => 'category.update', 'cn_name' => '分类更新', 'guard_name' => 'api'],

            ['name' => 'comment.index', 'cn_name' => '评论列表', 'guard_name' => 'api'],
            ['name' => 'comment.show', 'cn_name' => '评论详情', 'guard_name' => 'api'],
            ['name' => 'comment.reply', 'cn_name' => '评论回复', 'guard_name' => 'api'],

            ['name' => 'slider.index', 'cn_name' => '轮播图列表', 'guard_name' => 'api'],
            ['name' => 'slider.show', 'cn_name' => '轮播图详情', 'guard_name' => 'api'],
            ['name' => 'slider.store', 'cn_name' => '添加轮播图', 'guard_name' => 'api'],
            ['name' => 'slider.update', 'cn_name' => '轮播图更新','guard_name' => 'api'],
            ['name' => 'slider.destroy', 'cn_name' => '轮播图删除','guard_name' => 'api'],
            ['name' => 'slider.seq', 'cn_name' => '轮播图排序','guard_name' => 'api'],

            ['name' => 'goods.index', 'cn_name' => '商品列表', 'guard_name' => 'api'],
            ['name' => 'goods.show', 'cn_name' => '商品详情', 'guard_name' => 'api'],
            ['name' => 'goods.store', 'cn_name' => '添加商品', 'guard_name' => 'api'],
            ['name' => 'goods.update', 'cn_name' => '商品更新', 'guard_name' => 'api'],
            ['name' => 'goods.isOn', 'cn_name' => '商品上下架', 'guard_name' => 'api'],

            ['name' => 'image.store', 'cn_name' => '图片上传', 'guard_name' => 'api'],
            ['name' => 'image.storeBase64', 'cn_name' => '图片base64上传', 'guard_name' => 'api'],
            ['name' => 'image.destroy', 'cn_name' => '图片删除', 'guard_name' => 'api'],

            ['name' => 'order.index', 'cn_name' => '订单列表', 'guard_name' => 'api'],
            ['name' => 'order.show', 'cn_name' => '订单详情', 'guard_name' => 'api'],
            ['name' => 'order.post', 'cn_name' => '订单发货', 'guard_name' => 'api'],

        ];
        foreach ($permissions as $p) {
            Permission::create($p);
        }

        // 添加角色
        $role = Role::create(['name' => 'super-admin', 'cn_name' => '超级管理员', 'guard_name' => 'api']);

        // 为角色添加权限
        $role->givePermissionTo(Permission::all());
    }
}
